<template>
  <div class='postComment'>
    <van-field
      v-model.trim="message"
      rows="1"
      type="textarea"
      maxlength="100"
      placeholder="请输入留言"
      autosize
      show-word-limit
      class="commentInput"
    />
    <van-button
      type="primary"
      class="postBtn"
      :disabled="!message"
      @click="postComment"
    >发送</van-button>
  </div>
</template>

<script>
import { setArticleComment } from '@/api/comments'
import { Toast } from 'vant'

export default {
  name: 'postComment',
  components: {},
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      default: null
    }
  },
  data () {
    return {
      message: '' // 输入框内的内容
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async postComment () {
      Toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      const { data } = await setArticleComment({
        target: this.target.toString(), // 评论的目标id（评论文章即为文章id，对评论进行回复则为评论id）
        content: this.message, // 评论内容
        art_id: this.articleId // 文章id，对评论内容发表回复时，需要传递此参数，表明所属文章id。对文章进行评论，不要传此参数。
      })
      this.$emit('post-success', data.data.new_obj)
      Toast.success('评论成功')
      this.message = ''
    }
  }
}
</script>
<style lang='less' scoped>
.postComment {
  display: flex;
  padding: 14px;
  position: relative;
  background-color: #f6f6f6;
  .van-cell {
    padding: 0;
    width: 76%;
    /deep/ .van-field__control {
      min-height: 40px;
      background-color: #ffffff;
      padding: 8px 5px 5px 5px;
      font-size: 16px;
      border-radius: 5px;
    }
  }
}
.commentInput {
  background-color: #f8f8f8;
}
.postBtn {
  width: 70px;
  height: 38px;
  border-radius: 7px;
  margin-left: 10px;
  position: absolute;
  bottom: 34px;
  right: 14px;
  font-size: 16px;
}
</style>
